﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class SendGridMailSender : MailSender, IMailSender
    {
        /// <summary>
        /// The SMTP port
        /// </summary>
        protected int smtpPort = 25;

        #region Properties

        /// <summary>
        /// Gets or sets the SMTP address.
        /// </summary>
        /// <value>
        /// The SMTP address.
        /// </value>
        public new string SmtpAddress
        {
            get
            {
                return "smtp.sendgrid.net:" + smtpPort.ToString();
            }
            set
            {
                //do nothing.
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="SendGridMailSender" /> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="fullMailAddress">The full mail address.</param>
        /// <param name="smtpPort">The SMTP port.</param>
        public SendGridMailSender(Guid key, string userName, string password, string fullMailAddress, int? smtpPort = 25)
            : base(key, string.Empty, userName, password, fullMailAddress)
        {
            if (smtpPort != null)
            {
                this.smtpPort = smtpPort.Value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SendGridMailSender" /> class.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="fullMailAddress">The full mail address.</param>
        /// <param name="smtpPort">The SMTP port.</param>
        public SendGridMailSender(string userName, string password, string fullMailAddress, int? smtpPort = 25)
            : this(Guid.NewGuid(), userName, password, fullMailAddress, smtpPort)
        {
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Sends the mail.
        /// </summary>
        /// <param name="mailMessage">The mail message.</param>
        /// <param name="fromDisplay">From display.</param>
        /// <exception cref="OperationFailureException">SendGridSendMail</exception>
        public override void SendMail(MailMessage mailMessage, string fromDisplay)
        {        
            try
            {
                mailMessage.CheckNullObject("mailMessage");

                SmtpClient smtpClient = new SmtpClient();

                smtpClient.Credentials = new NetworkCredential(this.UserName, this.Password);
                this.SenderIdentity = this.UserName;
                if (this.SmtpAddress.Contains(':'))
                {
                    string[] splitted = this.SmtpAddress.Split(':');
                    smtpClient.Host = splitted[0];
                    smtpClient.Port = splitted[1].DBToInt32();
                }
                else
                {
                    smtpClient.Host = this.SmtpAddress;
                }
                mailMessage.From = new MailAddress(this.FullMailAddress, string.IsNullOrWhiteSpace(fromDisplay) ? "GBI" : fromDisplay);
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                throw new OperationFailureException("SendGridSendMail", ex, mailMessage);
            }
        }


        #endregion

    }
}
